<template>
    <div>
        <page-header @toLastPage="toLastPage">
        <template v-slot:title>
        个人信息 
      </template>
        </page-header>
         <div class="information">
            <div v-for="(item,index) in mineList" :key="index">
  <cell-list :itemList="item"  @sendEvent='sendEvent' @upLoadEvent='upLoadEvent'></cell-list>
    </div>
        </div>
    </div>
</template>
<script>
import pageHeader from '../common/pageHeader';
import cellList from '../common/cellList';
import {Meteor} from 'meteor/meteor';
import {user,image} from '../../../api/collections/index';
import upload from '../../../api/libs/upload';
export default {
     meteor: {
    $subscribe: {
      'userInf': []
    },
     userInf(){
        const id = sessionStorage.getItem('user')
        const getUser = user.findOne({_id:id})
        if(getUser){
          const avatar = image.findOne({_id:getUser.avatarId})
          if(avatar){
            getUser.avatar = avatar.base64    
          }
        }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
        return  getUser
    }
    },
    watch:{
      userInf(){
        this.mineList[0].moreList[0].avatar = this.userInf.avatar
        this.mineList[0].moreList[1].content = this.userInf.nickname
        this.mineList[0].moreList[3].content = this.userInf.weNum
      }
    },
    data() {
        return {
          weNum:'',
        mineList:[
         {
          separate:false,
         moreList:[
           {
              describe:'头像',
              icon:'',
              content:'',
              upLoad:true,
              avatar:'',
              event:'changeAvatar'
           },
           {
              describe:'昵称',
              icon:'',
              content:'',
              event:'changeNickname'
           },
            {
              describe:'拍一拍',
              icon:'',
              content:''
           },
           {
              describe:'微信号',
              icon:'',
              content:'',
              event:'changeWenum'
           },
           {
              describe:'二维码名片',
              icon:'',
              content:''
           },
            {
              describe:'更多',
              icon:'',
              content:''
           }
         ]
        },
        {
          separate:true,
          describe:'我的地址',
          icon:'',
          content:''
        }
        ]
        }
    },
     components:{
        pageHeader,
        cellList
    },
    methods:{
        toLastPage(){
            this.$router.go(-1)
        },
        sendEvent(e){
      switch(e){
          case 'changeAvatar':break;
          case 'changeNickname':this.$router.push(`/changeNickname/${this.mineList[0].moreList[1].content}`);break;
          case 'changeWenum':this.$router.push(`/changeWenum/${this.mineList[0].moreList[3].content}`);break;
        default:this.$notify({ type: 'success', message: '功能暂未开发',duration: 500 });break;
      }
    },
    upLoadEvent(e){
      upload.uploadImg(e,this.afterUpload)
    },
    afterUpload(res,imgcode){
       let userId = sessionStorage.getItem('user')
        let userInf = user.findOne({_id:userId})
        Meteor.call('image.update',{
            avatarId:userInf.avatarId,
            base64:imgcode
        })
    }
    }
}
</script>
<style scoped>
    .information{
        margin-top: 1.5rem;
    }
</style>